package ch.sfug.util {

	/**
	 * this class will display the memory usage of the swf movie. you can use it the following way:
	 * 
	 * <code>
	 * var mem:TextField = new TextField();
	 * mem.width = 300;
	 * addChild(mem);
	 * 
	 * Logger.appendOutput( new LogTextfield(mem), MemoryMonitor.LOG );
	 * 
	 * MemoryMonitor.start();
	 * </code>
	 * 
	 * note: you can use any LoggerOutput for displaying the memory info.
	 * 
	 * @author mich
	 */
	public class MemoryMonitor {

		private static var instance:MemoryMonitorInner;
		
		public static var LOG:String = "MemoryMonitorLog";

		private static function getInstance():MemoryMonitorInner {
			if( instance == null ) instance = new MemoryMonitorInner();
			return instance;
		}

		
		/**
		 * starts the memory monitor
		 */
		public static function start(  ):void {
			getInstance().start();
		}

		
		/**
		 * stops the memory monitor
		 */
		public static function stop(  ):void {
			getInstance().stop();
		}
	}
}

import flash.events.Event;
import flash.events.TimerEvent;
import flash.system.System;
import flash.utils.Timer;
import ch.sfug.util.MemoryMonitor;

import ch.sfug.log.Logger;

/**
 * @author mich
 */
class MemoryMonitorInner {

	private var timer:Timer;

	function MemoryMonitorInner() {
		timer = new Timer(200, 0);
		timer.addEventListener(TimerEvent.TIMER, updateMonitor);
	}

	
	private function updateMonitor( e:Event ):void {
		Logger.clear( MemoryMonitor.LOG );
		Logger.log( "System Memory: " + ( Math.round(( System.totalMemory / 1024 / 1024 ) * 100) / 100 ) + " MB", MemoryMonitor.LOG );
	}

	
	internal function start():void {
		timer.start();
	}

	
	internal function stop():void {
		timer.stop();
	}
}
